package cn.zktr.Personnel.model.service.zhouxiong;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.common.TimeStamp;
import cn.zktr.Personnel.model.dao.zhouxiong.*;
import cn.zktr.Personnel.model.mappers.zhouxiong.*;
import cn.zktr.Personnel.model.pojos.*;
import cn.zktr.Personnel.model.service.zhouyongcong.*;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitsaveService;

@Service
@Transactional(rollbackOn=Exception.class)
public class QiTaYeWuService {

	@Autowired
	private CwaKprMapper kprM;
	@Autowired
	private CwaQLaeDAO laeD;
	@Autowired
	private CwaQLaeMapper laeM;
	

	@Autowired
	private KaoQinKaService kqk;
	@Autowired
	private JiChuService jichu;
	@Autowired
	private findService fs;

	@Autowired
	private CwaResMapper resM;
	@Autowired
	private monitsaveService mss;
	@Autowired
	private boolService bs;
	
	//****************************审核********************************
	public void AuditQJ() {
		
	}
	
	
	
	/**签到次数
	 * @param EMPID
	 * @param DATE
	 * @return
	 */
	public int selectQianDaoCiShu(Integer EMPID,String DATE) {
		try {
			if(resM.selectQianDaoCiShu(EMPID, DATE)==null) {
				return 0;
			}
			return resM.selectQianDaoCiShu(EMPID, DATE).size();
		} catch (Exception e) {
			return -1;
		}
	}
	
	/**考勤前置  是否有考勤卡
	 * @param EMPID
	 * @return
	 */
	public int selectSFKqk(Integer EMPID) {
		try {
			if(kprM.selectSFKqk(EMPID)==null) {
				return 0;
			}else {
				return 1;
			}
		} catch (Exception e) {
			return -1;
		}
	}
	
	/**考勤主页 考勤次数 考勤总时间
	 * @param EMPID
	 * @param DATE
	 * @return
	 */
	public JSONObject selectTongJi(Integer EMPID,String DATE) {
		try {
			JSONObject json = resM.selectTongJi(EMPID);
			try {
				int RESTIME=0;
				List<JSONObject> list = kqk.seleYuanGongKaoQin(EMPID);
				for (JSONObject obj : list) {
					RESTIME += Integer.valueOf(obj.get("RESTIME").toString());
				}
				if(list.size()==0) {
					RESTIME=0;
				}
				json.put("RESTIME", RESTIME);
				
				String[] split = DATE.split("-");
				String dates = "";
				if(Integer.valueOf(split[1])<10) {
					dates = split[0]+"-0"+split[1];
				}else {
					dates = DATE;
				}
				JSONArray ciShu = resM.selQinJiaCiShu(EMPID, dates);
				json.put("QJCiShu", ciShu.size());
				
				JSONArray jiabansj = resM.KaoQinShuJuQianZhiQianZhi3_Fu_RESTIME(EMPID, dates, 0);
				Integer JBShuChang = 0;
				for (int i = 0; i < jiabansj.size(); i++) {
					JBShuChang += jiabansj.getJSONObject(i).getInteger("sub_seconds");
				}
				json.put("JBShuChang", JBShuChang);
				
				return json;
			} catch (NullPointerException e) {
				return json;
			}
		} catch (Exception e) {
			return null;
		}
	}

	/**请假列表
	 * @return
	 */
	public JSONArray seleQinJia() {
		try {
			return laeM.seleQinJia();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**根据员工id查询请假 统计
	 * @param EMPID
	 * @return
	 */
	public JSONArray seleQinJiaEmp(Integer EMPID) {
		try {
			return laeM.seleQinJiaEmp(EMPID);
		} catch (Exception e) {
			return null;
		}
	}
	
	/**全部员工 统计请假时长
	 * @param EMPID
	 * @return
	 */
	public JSONArray seleQinJiaQB() {
		try {
			return laeM.seleQinJiaQB();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**根据员工统计 请假时长
	 * @param EMPID
	 * @return
	 */
	public JSONArray seleQinJiaQBEmp(Integer EMPID) {
		try {
			return laeM.seleQinJiaQBEmp( EMPID);
		} catch (Exception e) {
			return null;
		}
	}
	
	/**新增请假
	 * @param EMPID
	 * @param TYPEID
	 * @param LAEMINTIME
	 * @param LAEMAXTIME
	 * @param LAEREM
	 */
	public String updateQJ(Integer EMPID,Integer TYPEID,String LAEMINTIME,String LAEMAXTIME,String LAEREM) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date s1 = df.parse(LAEMINTIME+":00");
			Date s2 = df.parse(LAEMAXTIME+":00");
			long sl1 = s1.getTime();
			long sl2 = s2.getTime();
			long time = new TimeStamp().getNetworkTime().getTime();
			if(sl1<time || sl2<time || sl1>sl2) {
				return "2";
			}
			if(laeM.seleQinJiaYanZhen(EMPID, LAEMINTIME, LAEMAXTIME).size()>0) {
				return "1";
			}
			PerCwaQLae lae = new PerCwaQLae();
			lae.setLaeid(bs.bool_QLae());
			lae.setLaemintime(Timestamp.valueOf(LAEMINTIME+":00"));
			lae.setLaemaxtime(Timestamp.valueOf(LAEMAXTIME+":00"));
			lae.setLaerem(LAEREM);
			lae.setPerCwaCwa(kqk.findByEmpid(EMPID));
			lae.setPerCwaQType(jichu.findByQJZongLeiId(TYPEID));
			lae.setPerPmsEmp(fs.findEmpid(EMPID));
			mss.save_08(EMPID, lae);
			return "0";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
}
